package com.evon.yardmanagement.service;

import java.util.Collections;
import java.util.List;

import com.evon.yardmanagement.dao.MemberDAO;
import com.evon.yardmanagement.model.Member;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.ui.dto.MemberDTO;

/**
 * Service class for interacting with DAO layers for member data.
 * 
 * @author vgaurav
 * 
 */
public class MemberService {

	BaseTransformer<Member, MemberDTO> transformer = new BaseTransformer<Member, MemberDTO>(
			Member.class, MemberDTO.class);

	MemberDAO memberDAO = new MemberDAO();

	/**
	 * Return all un occupied members of the given yard.
	 * 
	 * @param yardId
	 * @return
	 */
	public List<MemberDTO> getUnOccupiedMembersOfYard(int yardId) {
		List<Member> listMemberModel = memberDAO.getUnOccupiedMembers(yardId);
		if (listMemberModel == null || listMemberModel.isEmpty())
			return Collections.EMPTY_LIST;
		return transformer.transformToDTO(listMemberModel);
	}

	public List<MemberDTO> getMembersOfYard(int yardId) {
		List<Member> listMemberModel = memberDAO.getMembers(yardId);
		if (listMemberModel == null || listMemberModel.isEmpty())
			return Collections.EMPTY_LIST;
		return transformer.transformToDTO(listMemberModel);
	}

	public MemberDTO getMemberDetails(int id) {
		// TODO Auto-generated method stub
		Member member = memberDAO.findById(id);
		return transformer.transformToDTO(member);
	}
	
	public MemberDTO addMember(MemberDTO dto, int yardId){
		Member dirtyMemberObj = memberDAO.get(dto.getId());
		if(dirtyMemberObj == null){
			dirtyMemberObj = new Member();
		}
		Member member = transformer.transformToModel(dto, dirtyMemberObj);
		member.setYardId(yardId);
		memberDAO.saveOrUpdate(member);
		return transformer.transformToDTO(member);
	}
	
}
